package org.crystalrange.documents.web;

import org.crystalrange.documents.model.Document;
import org.crystalrange.documents.model.DocumentReference;
import org.crystalrange.documents.model.DocumentReferenceType;
import org.crystalrange.documents.model.DocumentVersion;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class DocumentReferenceController extends GenericForwardComposer {

	private DocumentReference documentReference;
	private DocumentReferenceType documentReferenceType;
	private Document parentDocument;
	private Document childDocument;
	private DocumentVersion parentDocumentVersion;
	private DocumentVersion childDocumentVersion;
	protected Bandbox bandboxDocumentReferenceType;
	protected Bandbox bandboxParentDocument;
	protected Bandbox bandboxChildDocument;
	protected Bandbox bandboxParentDocumentVersion;
	protected Bandbox bandboxChildDocumentVersion;
	protected AnnotateDataBinder binder;
	private String documentDisabled;
	private String documentVersionDisabled;

	public DocumentReference getDocumentReference() {
		return documentReference;
	}

	public void setDocumentReference(DocumentReference documentReference) {
		this.documentReference = documentReference;
	}

	public String getDocumentDisabled() {
		return documentDisabled;
	}

	public void setDocumentDisabled(String documentDisabled) {
		this.documentDisabled = documentDisabled;
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);
		this.documentReference = (DocumentReference) this.execution
				.getAttribute("documentReference");
		
		this.documentDisabled = (String) this.execution.getAttribute("documentDisabled");
		this.documentVersionDisabled = (String) this.execution.getAttribute("documentVersionDisabled");
		
		if (this.documentDisabled == "childDocument") {
			bandboxChildDocument.setDisabled(true);
		} else if (this.documentDisabled == "parentDocument") {
			bandboxParentDocument.setDisabled(true);
		}
		
		if (this.documentVersionDisabled == "childDocumentVersion") {
			bandboxChildDocumentVersion.setDisabled(true);
		} else if (this.documentVersionDisabled == "parentDocumentVersion") {
			bandboxParentDocumentVersion.setDisabled(true);
		}
		
		this.saveData();
		
		component.setAttribute("controller", this);
	}

	public void onClick$buttonOk(Event event) throws Exception {
		if (this.documentReference.getDocumentReferenceType() != null && 
			this.documentReference.getChildDocument() != null && 
			this.documentReference.getParentDocument() != null) {
			
			this.execution.setAttribute("documentReference", this.documentReference);
			this.execution.setAttribute("result", "ok");
			this.self.detach();
		} else {
			Messagebox.show("You must select 'Reference Type', 'Child Document' and 'Parent Document'.");
		}
	}

	public void onClick$buttonCancel(Event event) throws Exception {
		this.restoreData();
		
		this.execution.setAttribute("documentReference", null);
		this.execution.setAttribute("result", "cancel");
		this.self.detach();
	}

	public void onOpen$bandboxDocumentReferenceType(Event event)
			throws Exception {
		Window window = (Window) this.execution.createComponents(
				"~./zul/html/documentReferenceTypeSelect.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			DocumentReferenceType selectedDocumentReferenceType = (DocumentReferenceType) this.execution.getAttribute("selectedDocumentReferenceType");
			this.documentReference.setDocumentReferenceType(selectedDocumentReferenceType);
		}

		this.binder.loadAll();
	}
	
	public void onOpen$bandboxParentDocument(Event event)
			throws Exception {
		Window window = (Window) this.execution.createComponents(
				"~./zul/html/documentSelect.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			Document selectedDocument = (Document) this.execution.getAttribute("selectedDocument");
			this.documentReference.setParentDocument(selectedDocument);
			this.documentReference.setParentDocumentVersion(null);
		}

		this.binder.loadAll();
	}
	
	public void onOpen$bandboxParentDocumentVersion(Event event)
			throws Exception {
		Window window = (Window) this.execution.createComponents(
				"~./zul/html/documentSelect.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			Document selectedDocument = (Document) this.execution.getAttribute("selectedDocument");
			this.execution.setAttribute("document", selectedDocument);
			
			window = (Window) this.execution.createComponents(
				"~./zul/html/documentVersionSelect.zul", null, null);
			window.setParent(self);
			window.doModal();
			
			if (this.execution.getAttribute("result") == "ok") {
				DocumentVersion selectedDocumentVersion = (DocumentVersion) this.execution.getAttribute("selectedDocumentVersion");
				this.documentReference.setParentDocumentVersion(selectedDocumentVersion);
				this.documentReference.setParentDocument(selectedDocument);
			}
		}
		
		this.binder.loadAll();
		
	}
	
	public void onOpen$bandboxChildDocument(Event event)
			throws Exception {
		Window window = (Window) this.execution.createComponents(
				"~./zul/html/documentSelect.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			Document selectedDocument = (Document) this.execution.getAttribute("selectedDocument");
			this.documentReference.setChildDocument(selectedDocument);
			this.documentReference.setChildDocumentVersion(null);
		}

		this.binder.loadAll();
	}
	
	public void onOpen$bandboxChildDocumentVersion(Event event)
			throws Exception {
		Window window = (Window) this.execution.createComponents(
				"~./zul/html/documentSelect.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			Document selectedDocument = (Document) this.execution.getAttribute("selectedDocument");
			this.execution.setAttribute("document", selectedDocument);
			
			window = (Window) this.execution.createComponents(
				"~./zul/html/documentVersionSelect.zul", null, null);
			window.setParent(self);
			window.doModal();
			
			if (this.execution.getAttribute("result") == "ok") {
				DocumentVersion selectedDocumentVersion = (DocumentVersion) this.execution.getAttribute("selectedDocumentVersion");
				this.documentReference.setChildDocumentVersion(selectedDocumentVersion);
				this.documentReference.setChildDocument(selectedDocument);
			}
		}
		
		this.binder.loadAll();
	}
	
	private void saveData() {
		this.documentReferenceType = this.documentReference.getDocumentReferenceType();
		this.parentDocument = this.documentReference.getParentDocument();
		this.childDocument = this.documentReference.getChildDocument();
		this.parentDocumentVersion = this.documentReference.getParentDocumentVersion();
		this.childDocumentVersion = this.documentReference.getChildDocumentVersion();
	}

	private void restoreData() {
		this.documentReference.setDocumentReferenceType(this.documentReferenceType);
		this.documentReference.setParentDocument(this.parentDocument);
		this.documentReference.setChildDocument(this.childDocument);
		this.documentReference.setParentDocumentVersion(this.parentDocumentVersion);
		this.documentReference.setChildDocumentVersion(this.childDocumentVersion);
	}

}
